﻿
Public Class frmBusquedaSOAP
    Inherits frmMaster

    Private MiRUT As String
    Private MiNroSeguro As String
    Private MiPatente As String
    Private MiFechaDesde As String
    Private MiFechaHasta As String
    Private MiEstado As String

    Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            txtRUTCliente.Attributes.Add("onkeypress", "return isNumber(event)")
            txtRUTCliente.Attributes.Add("onkeydown", "return isNumber(event)")
            txtDVRUTCliente.Attributes.Add("onkeydown", "return isAlphaNumber(event);")
            txtDVRUTCliente.Attributes.Add("onkeypress", "return isAlphaNumber(event);")
            txtDVRUTCliente.Attributes.Add("style", "text-transform: uppercase")
            txtPatente.Attributes.Add("onkeydown", "return isAlphaNumber(event);this.value=this.value.toUpperCase();")
            txtPatente.Attributes.Add("onkeypress", "return isAlphaNumber(event);this.value=this.value.toUpperCase();")
            txtPatente.Attributes.Add("style", "text-transform: uppercase")
            txtNroSeguro.Attributes.Add("onkeypress", "return isNumber(event)")
            txtNroSeguro.Attributes.Add("onkeydown", "return isNumber(event)")
            txtFechaInicio.Attributes.Add("onkeydown", "return isValidDatechar(event)")
            txtFechaInicio.Attributes.Add("onkeypress", "return isValidDatechar(event)")

            MiRUT = Nothing
            MiNroSeguro = Nothing
            MiPatente = Nothing
            MiFechaDesde = Nothing
            MiFechaHasta = Nothing
            MiEstado = Nothing

            GetListadoSOAPGratis()
        End If
    End Sub

    Protected Sub btnSalir_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSalir.Click
        Session.Abandon()
        IrA(ConfigurationManager.AppSettings("InicioBackEnd"))
    End Sub

    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExcel.Click
        Export2Excel(dgSOAP)
    End Sub

    Private Sub Export2Excel(ByRef MiGrilla As Anthem.DataGrid)
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=" & ConfigurationManager.AppSettings("ArchivoExcel") & ".xls")
        Response.Charset = ""
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentEncoding = System.Text.Encoding.UTF8
        Response.ContentType = "application/vnd.ms-excel"
        Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)

        FormatearGrilla(MiGrilla)

        MiGrilla.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString())
        Response.End()
    End Sub

    Protected Sub FormatearGrilla(ByRef MiGrilla As Anthem.DataGrid)
        With MiGrilla.HeaderStyle
            .HorizontalAlign = HorizontalAlign.Left
            .ForeColor = System.Drawing.ColorTranslator.FromHtml("#000066")
            .BackColor = System.Drawing.ColorTranslator.FromHtml("#C6D5EC")
            .Font.Bold = True
        End With
    End Sub

    Protected Sub GetListadoSOAPGratis()
        GetListadoSOAPGratis(dgSOAP, MiRUT, MiPatente, MiNroSeguro, MiFechaDesde, MiFechaHasta, MiEstado)
    End Sub

    Protected Sub GetListadoSOAPGratis(ByRef MiGrilla As Anthem.DataGrid, ByVal ParamArray Parametros() As String)
        Dim rp As New DotNetResponse.SQLPersistence

        If Not Parametros Is Nothing Then
            rp.Parametro = Parametros
        End If

        rp.StoredProcedure = "SP_GET_LISTA_SOAP_GRATIS"

        ExecuteSP(rp, False, True)

        MiGrilla.DataSource = Nothing
        MiGrilla.DataBind()
        MiGrilla.UpdateAfterCallBack = True

        If Not rp.Errores Then
            If Not rp.DtTable Is Nothing Then
                MiGrilla.DataSource = rp.DtTable
                MiGrilla.DataBind()
                MiGrilla.UpdateAfterCallBack = True
            End If
        End If

        rp.Dispose()
    End Sub

    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBuscar.Click
        Dim sErrores As String = String.Empty
        Dim Sigue As Boolean = True

        If (txtRUTCliente.Text.Trim <> "" Or txtDVRUTCliente.Text.Trim <> "") Then
            If Not ValidaRut(txtRUTCliente.Text.Trim & "-" & txtDVRUTCliente.Text.Trim) Then
                Sigue = False
                sErrores = "RUT No válido" & "\n"
            End If
        End If

        If txtFechaInicio.Text.Trim <> "" And Not IsDate(txtFechaInicio.Text.Trim) Then
            Sigue = False
            sErrores = "Fecha Inicio No válida" & "\n"
        End If

        If txtFechaTermino.Text.Trim <> "" And Not IsDate(txtFechaTermino.Text.Trim) Then
            Sigue = False
            sErrores = "Fecha Termino No válida" & "\n"
        End If

        If IsDate(txtFechaInicio.Text.Trim) And IsDate(txtFechaTermino.Text.Trim) Then
            If DateDiff(DateInterval.Day, Convert.ToDateTime(txtFechaInicio.Text.Trim), Convert.ToDateTime(txtFechaTermino.Text.Trim)) < 0 Then
                Sigue = False
                sErrores = "Ingrese un intervalo correcto de fechas.-" & "\n"
            End If
        End If

        If Sigue Then
            MiRUT = IIf((txtRUTCliente.Text.Trim = "" And txtDVRUTCliente.Text.Trim = ""), Nothing, txtRUTCliente.Text.Trim & "-" & txtDVRUTCliente.Text.Trim)
            MiNroSeguro = IIf(txtNroSeguro.Text.Trim = "", Nothing, txtNroSeguro.Text.Trim)
            MiPatente = IIf(txtPatente.Text.Trim = "", Nothing, txtPatente.Text.Trim)
            MiFechaDesde = IIf(txtFechaInicio.Text.Trim = "", Nothing, txtFechaInicio.Text.Trim)
            MiFechaHasta = IIf(txtFechaTermino.Text.Trim = "", Nothing, txtFechaTermino.Text.Trim)
            MiEstado = IIf(ddlEstado.SelectedValue = "-999", Nothing, ddlEstado.SelectedValue)
            GetListadoSOAPGratis()
        Else
            MsgError(sErrores)
        End If
    End Sub

    Protected Sub btnLimpiar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLimpiar.Click
        txtRUTCliente.Text = ""
        txtRUTCliente.UpdateAfterCallBack = True
        txtDVRUTCliente.Text = ""
        txtDVRUTCliente.UpdateAfterCallBack = True
        txtNroSeguro.Text = ""
        txtNroSeguro.UpdateAfterCallBack = True
        txtPatente.Text = ""
        txtPatente.UpdateAfterCallBack = True
        txtFechaInicio.Text = ""
        txtFechaInicio.UpdateAfterCallBack = True
        txtFechaTermino.Text = ""
        txtFechaTermino.UpdateAfterCallBack = True
        ddlEstado.SelectedIndex = 0
        ddlEstado.UpdateAfterCallBack = True

        btnBuscar_Click(Nothing, Nothing)

        Anthem.Manager.AddScriptForClientSideEval("Calendar.setup({inputField: 'txtFechaInicio',ifFormat: '%d/%m/%Y',button: 'imFechaInicio',align: 'Tl',singleClick: true});function imFechaInicio_onclick() {}")
        Anthem.Manager.AddScriptForClientSideEval("Calendar.setup({inputField: 'txtFechaTermino',ifFormat: '%d/%m/%Y',button: 'imFechaTermino',align: 'Tl',singleClick: true});function imFechaTermino_onclick() {}")
    End Sub

    Protected Sub dgSOAP_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSOAP.ItemDataBound
        If e.Item.ItemType = ListItemType.Header Then
            e.Item.Cells(4).Text = HttpUtility.HtmlEncode(e.Item.Cells(4).Text)
            e.Item.Cells(7).Text = HttpUtility.HtmlEncode(e.Item.Cells(7).Text)
        ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.Cells(2).Text = HttpUtility.HtmlEncode(e.Item.Cells(2).Text)
        End If
    End Sub
End Class
